package cn.superfw.filedownload;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;
import org.apache.commons.net.ftp.FTPReply;

/**
 * 通用FTP资源下载器
 */
public class FTPFileDownloader {

	private String username;
	private String password;
	private String ip;
	private int port;
	private FTPClient ftpClient;

	public FTPFileDownloader(String username, String password, String ip, int port) {
		super();
		this.username = username;
		this.password = password;
		this.ip = ip;
		this.port = port;
	}

	/**
	 * 连接FTP服务器
	 * @return 连接结果
	 * @throws Exception 异常
	 */
	public boolean connectServer() throws Exception{

		if (ftpClient == null) {
			ftpClient = new FTPClient();
			ftpClient.connect(ip,port);

			System.out.println("Connected to " + ip);
			System.out.println(ftpClient.getReplyString());

			int reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
				System.err.println("FTP server refused connection.");
				return false ;
			}

			boolean login = ftpClient.login(username, password);
			if (!login)  {
				try {
					ftpClient.disconnect() ;
					ftpClient = null ;
				} catch (Exception e) { }
				throw new Exception("can not login ftp server") ;
			}

			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("UTF-8");
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			ftpClient.setDataTimeout(120000);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setUseEPSVwithIPv4(false);
		}
		return true;
	}


	/**
	 * 列出远程文件目录
	 * @param path 远程目录
	 * @return 文件列表
	 * @throws Exception 异常
	 */
	public List<String> listRemoteAllFiles(String path) throws Exception {
		ftpClient.enterLocalPassiveMode();
		FTPFile[] files = ftpClient.listFiles(path, new FTPFileFilter() {
			public boolean accept(FTPFile file) {
				if (file.isFile()) {
					return true ;
				}
				return false ;
			}}) ;

		List<String> list = new ArrayList<String>() ;
		for (FTPFile file : files) {
			list.add(file.getName()) ;
		}
		return list ;
	}

	/**
	 * 下载文件
	 * @param remotePath 远程目录
	 * @param fileName 文件名
	 * @param localPath 本地目录
	 * @return 下载结果
	 * @throws Exception 异常
	 */
	public boolean downloadFile(String remotePath, String fileName, String localPath) throws Exception {

		FileOutputStream fos = null ;
		try {
			File localFile = new File(localPath, fileName);
			fos = new FileOutputStream(localFile);

			ftpClient.enterLocalPassiveMode();
			ftpClient.changeWorkingDirectory(remotePath) ;
			boolean flag = ftpClient.retrieveFile(fileName, fos);

			fos.close() ;
			fos = null ;

			return flag ;
		} catch (Exception e) {
			throw e ;
		}
		finally {
			if (fos != null) {
				try {
					fos.close() ;
					fos = null ;
				} catch (Exception e2) { }
			}
		}

	}

	/**
	 * 切断服务器连接
	 */
	public void closeConnect() {
		try {
			if (ftpClient != null) {
				ftpClient.logout();
				ftpClient.disconnect();
			}
		} catch (Exception e) {
		}

	}

}
